package cn.ssm.test.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;

import cn.ssm.test.model.TestVO;
import cn.ssm.test.model.User;
import cn.ssm.test.service.IUserService;

  
    
@Controller    
@RequestMapping("/user")    
public class UserController {    
    @Resource    
    private IUserService userService;    
    @RequestMapping("/userList")    
    public String userList(HttpServletRequest request,Model model){    
        List<User> uList = userService.getAllUser();    
        model.addAttribute("uList", uList);    
        return "userList";    
    }    
    @RequestMapping("/login")    
    public String login(HttpServletRequest request,Model model){
    	
    	String username = request.getParameter("name");
    	if (username != null) {
    		User user = userService.getUserByAccount(username);
    		if (user.getPassword().equals(request.getParameter("password"))) {
        		request.getSession().setAttribute(username, JSON.toJSONString(user));
        		model.addAttribute("user", user);
        		System.err.println(username);
        		System.err.println(request.getSession().getAttribute(username));
        		System.err.println(request.getSession().getId());
        		model.addAttribute("sessionId", request.getSession().getId());
        		return "indexuser";
    		} else {
    			return "index";
    		}
		}
    	
    	return "index";
    }    
       
    @RequestMapping("/loginout")    
    public String loginout(HttpServletRequest request,Model model){
    	// String name = request.getParameter("name");
    	request.getSession().removeAttribute("user");
    	
    	return "index";
    } 
    
    @RequestMapping("/showUser")    
    public String showUser(HttpServletRequest request,Model model){    
        int userId = Integer.parseInt(request.getParameter("id"));    
        User user = userService.getUserById(userId);    
        model.addAttribute("user", user);    
        return "showUser";    
    }    
        
    @RequestMapping("/userInfo")    
    public String userInfo(HttpServletRequest request,Model model){
    	//String name = request.getParameter("name");
    	String s = (String) request.getSession().getAttribute("user");
    	//String s = (String) request.getSession().getAttribute(name);
    	System.err.println("-------"+s);
    	model.addAttribute("user", JSON.parseObject(s, User.class));
        return "userinfo";    
    }    
    @RequestMapping("/index")    
    public String index(Model model){  

        return "index";    
    }    
    @RequestMapping("/addUser")    
    public String addUser(HttpServletRequest request,Model model){    
        User user = new User();    
        user.setName(String.valueOf(request.getParameter("name")));    
        user.setPassword(String.valueOf(request.getParameter("password")));    
       // user.setAge(Integer.parseInt(String.valueOf(request.getParameter("age"))));    
        userService.addUser(user);    
        return "redirect:/user/userList";    
    }    
}
